
/*
 * *****************************************************************************************
 *
 * 		Copyright (C) 2014-2019 Gowin Semiconductor Technology Co.,Ltd.
 * 		
 * @file			main.c
 * @author		Embedded Development Team
 * @version		V1.0.0
 * @date			2019-10-1 09:00:00
 * @brief			Main program body.
 ******************************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "gw1ns4c.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void UartInit(void);
void SPIInit(void);
void Delay_ms(__IO uint32_t delay_ms);

int main(void)
{
  SystemInit();
	SPIInit();
	UartInit();
	
  GPIO0 ->OUTENSET = 0xFFFFFFFF;
  GPIO0 ->DATAOUT = 0x0;
	
  SPI_Select_Slave(0x01) ;//Select The SPI Slave	
  SPI_WriteData(0x9F);//Send Jedec
	
  while(1)
  {
		if(~SPI_GetToeStatus() && SPI_GetTrdyStatus() == 1)
		{
			  SPI_WriteData(0x9F);//Send Jedec
		}
		if(~SPI_GetRoeStatus() && SPI_GetRrdyStatus() == 1)
		{
			  UART_SendChar(UART0,SPI_ReadData());
		}
  }
   
}

void SPIInit(void)
{
	SPI_InitTypeDef init_spi;
	
  init_spi.CLKSEL= CLKSEL_CLK_DIV_8;//50MHZ / 8
  init_spi.DIRECTION = DISABLE;//MSB First
  init_spi.PHASE =DISABLE;//ENABLE;//posedge
  init_spi.POLARITY =DISABLE;//ENABLE;//polarity 0
	
  SPI_Init(&init_spi);
}

void UartInit(void)
{
  UART_InitTypeDef UART_InitStruct;
	
  UART_InitStruct.UART_Mode.UARTMode_Tx = ENABLE;
  UART_InitStruct.UART_Mode.UARTMode_Rx = ENABLE;
  UART_InitStruct.UART_Int.UARTInt_Tx = DISABLE;
  UART_InitStruct.UART_Int.UARTInt_Rx = DISABLE;
  UART_InitStruct.UART_Ovr.UARTOvr_Tx = DISABLE;
  UART_InitStruct.UART_Ovr.UARTOvr_Rx = DISABLE;
  UART_InitStruct.UART_Hstm = DISABLE;
  UART_InitStruct.UART_BaudRate = 9600;//Baud Rate
	
  UART_Init(UART0,&UART_InitStruct);
}

void Delay_ms(__IO uint32_t delay_ms)
{
	for(delay_ms=(SystemCoreClock>>13)*delay_ms; delay_ms != 0; delay_ms--);
}
